import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JTextArea;

/** StatusBox Class
    CS158A Spring 2010 Project #2
    @author Clare Lanam and Loc Phan
    */

public class StatusBox extends JTextArea
{
   /** serial ID added by Eclipse compiler */
   private static final long serialVersionUID = 1L;
   
   // ------[CONSTRUCTOR]------ //
   
   public StatusBox()
   {      
      setFont(new Font("Arial", Font.PLAIN, 12));
      setBackground(Color.LIGHT_GRAY);
      setForeground(Color.BLACK);
      setLineWrap(true);
      setEditable(false);
      setFocusable(false);
   }
   
   // ------[PUBLIC METHODS]------ //
 
   /** Clear the box. */
   
   public void clear() { setText(""); }
   
   /** Show info on a given bridge. */
   
   public void showBridgeStatus(BridgeIcon b)
   {
      StringBuilder wallaby = new StringBuilder();
      
      wallaby.append(" BRIDGE: ");
      wallaby.append(b.getID());
      if (b.getBridge().isRoot())
         wallaby.append(" [ROOT]");
      
      // ------:who does the bridge think the root is?:------
      
      wallaby.append("\n Root is Bridge ");
      wallaby.append(b.getBridge().getStoredRootBridgeID());
      wallaby.append(" (Path cost: ");
      wallaby.append(b.getBridge().getPathCostToRoot());
      wallaby.append(")");
      
      // ------:show port data:------
      
      LinkedList<Port> ports = b.getBridge().getPorts();
      Iterator<Port> iter = ports.iterator();
      wallaby.append("\n");
      
      while (iter.hasNext())
      {
         Port p = iter.next();
         
         wallaby.append("\n");
         wallaby.append(" Port connected to ");
         Link link = p.getLink();
         ArrayList<Port> portsOnLink = link.getPorts();
         
         // code assumes only 2 ports on link:
         for (Port pLink : portsOnLink)
            if (pLink != p)
               wallaby.append("[Bridge " + pLink.getBridge().getID());
         
         wallaby.append("] has status [");
         PortState ps = p.getState();
         wallaby.append(ps);
         wallaby.append(" -- ");
         wallaby.append(p.getRole());
         wallaby.append("] at path cost ");
         if (p.getPathCost() == Integer.MAX_VALUE)
            wallaby.append("unknown");
         else wallaby.append(p.getPathCost());
      }
      
      setText(wallaby.toString());
      setCaretPosition(0);
   }
   
   /** Show status on a pair of bridges. */
   
   public void showLinkStatus(BridgeIcon a, BridgeIcon b, boolean isLinked)
   {
      if (isLinked)
         setText(" Bridge " + a.getID() + " has a direct connection "
               + "to bridge " + b.getID() + ".");
      else
         setText(" Bridge " + a.getID() + " and bridge "
               + b.getID() + " are not directly connected.");
   }
}
